package webapp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Database;
import model.Item;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class InventoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String pathInfo = req.getPathInfo();
		//System.out.println("pathInfo=" + pathInfo);
		
		if (pathInfo != null && !pathInfo.equals("/")) {
			// If the pathInfo is "/Apples" it means that the
			// client wants the Item whose name is "Apples".
			String itemName = pathInfo.substring(1);
			
			// Accessing an Item
			
			Item item = Database.getInstance().getInventory().getItem(itemName);
			if (item ==  null) {
				// No such item: send a NOT_FOUND response
				notFound("No such item: " +  itemName, resp);
				return;
			} else {
				// Found the Item 
				Document doc = DocumentHelper.createDocument();
				item.toElement(doc);
				writeXML(doc, resp);
				return;
			}
		} else {
			// Accessing the entire Inventory
			Document doc = DocumentHelper.createDocument();
			Database.getInstance().getInventory().toElement(doc);
			writeXML(doc, resp);
			return;
		}
	}
	
	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String pathInfo = req.getPathInfo();
		
		if (pathInfo != null && !pathInfo.equals("/")) {
			// Replace a single item
			String itemName = pathInfo.substring(1);
			
		} else {
			// Accessing the entire inventory
			
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
	
	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doDelete(req, resp);
	}
	
	private void writeXML(Document doc, HttpServletResponse resp) throws IOException {
		OutputFormat format = OutputFormat.createPrettyPrint();
		resp.setContentType("text/xml");
		XMLWriter writer = new XMLWriter(resp.getWriter(), format);
		writer.write(doc);
	}

	private void notFound(String message, HttpServletResponse resp)
			throws IOException {
		resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
		resp.setContentType("text/plain");
		resp.getWriter().println(message);
	}
	
	private void badRequest(String message, HttpServletResponse resp) throws IOException {
		resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
		resp.setContentType("text/plain");
		resp.getWriter().println(message);
	}

	private void success(String message, HttpServletResponse resp) throws IOException {
		resp.setStatus(HttpServletResponse.SC_OK);
		resp.setContentType("text/plain");
		resp.getWriter().println(message);
	}
}
